﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using Microsoft.Extensions.Configuration;
using Simple.Http;
using Simple.Tools;
using SimpleSaas.Common;
using SimpleSaas.Web.Common.redis;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace SimpleSaas.Web.Common
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public static class AutofacRegisterExtension
	{
		/// <summary>
		/// 注册数据库和业务模块
		/// </summary>
		/// <param name="containerBuilder"></param>
		public static void RegisterDateBaseService(this ContainerBuilder containerBuilder)
		{

			string rootPath = AppDomain.CurrentDomain.BaseDirectory;
			string resDllPath = Path.Combine(rootPath, "Simple.SysRepository.dll");
			string iResDllPath = Path.Combine(rootPath, "Simple.ISysRepository.dll");
			string interfaceDllPath = Path.Combine(rootPath, "Simple.ISysService.dll");
			string serviceDllPath = Path.Combine(rootPath, "Simple.SysService.dll");
			Assembly assemblyRepository = Assembly.LoadFrom(resDllPath);
			Assembly assemblyIRepository = Assembly.LoadFrom(iResDllPath);
			Assembly assemblyService = Assembly.LoadFrom(serviceDllPath);
			Assembly assemblyIterface = Assembly.LoadFrom(interfaceDllPath);


			string connectionStr = App.Configuration.GetValue<string>("ConnectionStrings:MySqlConString");


			List<ConnectionConfig> connectionConfigs = new List<ConnectionConfig>();


			ConnectionConfig connectionConfig = new ConnectionConfig()
			{
				DbType = DbType.MySql,
				ConnectionString = connectionStr,
				ConfigId = "Default",
				IsAutoCloseConnection = true,
			};

			ISqlSugarClient dbClient = new SqlSugarClient(connectionConfig);


			containerBuilder.RegisterInstance(connectionConfig).SingleInstance().PropertiesAutowired(new AutowiredPropertySelector());

			containerBuilder.RegisterType<SqlSugarClient>().As<ISqlSugarClient>().InstancePerLifetimeScope().PropertiesAutowired(new AutowiredPropertySelector());

			#region 标记接口支持aop
			containerBuilder.RegisterAssemblyTypes(assemblyRepository, assemblyIRepository).
				AsImplementedInterfaces().PropertiesAutowired(new AutowiredPropertySelector()).
				EnableInterfaceInterceptors();

			containerBuilder.RegisterAssemblyTypes(assemblyService, assemblyIterface).
				PropertiesAutowired(new AutowiredPropertySelector()).AsImplementedInterfaces()
				.EnableInterfaceInterceptors();

			#endregion 标记接口支持aop

		}
		/// <summary>
		/// 注册工具服务模块
		/// </summary>
		/// <param name="containerBuilder"></param>
		public static void RegisterToolService(this ContainerBuilder containerBuilder)
		{
			containerBuilder.RegisterType<SimpleMapper>().As<ISimpleMapper>().SingleInstance();

			containerBuilder.RegisterType<HttpHelper>().As<IHttpHelper>().SingleInstance();

			#region httpHelper http://192.168.10.100:9000
			HttpHelper.AddClient("AuthenticateService", "http://192.168.10.100:9000");
			#endregion
		}

		/// <summary>
		/// 注册工具服务模块
		/// </summary>
		/// <param name="containerBuilder"></param>
		public static void RegisterRedis(this ContainerBuilder containerBuilder)
		{
			containerBuilder.RegisterType<RedisClientService>().As<IRedisClientService>().
			  PropertiesAutowired(new AutowiredPropertySelector())
			 .SingleInstance().EnableInterfaceInterceptors();
		}
	}
}